package cn.itcast.service.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ContractProductDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.domain.vo.ContractProductVo;
import cn.itcast.service.ContractService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.UUID;

//合同
@Service
public class ContractServiceImpl implements ContractService {

    @Autowired
    private ContractDao contractDao;
    @Autowired
    private ExtCproductDao extCproductDao;
    @Autowired
    private ContractProductDao contractProductDao;

    @Override
    public PageInfo findAll(ContractExample example, int page, int size) {
        /*分页查询*/
       PageHelper.startPage(page,size);
        List<Contract> contracts = contractDao.selectByExample(example);
        PageInfo pb=new PageInfo(contracts);
        return pb;
    }


    @Override
    public Contract findById(String id) {
        Contract contract = contractDao.selectByPrimaryKey(id);
        return contract;
    }

    @Override
    public void save(Contract contract) {
        /*设置状态  0：草稿 ，1:已准备上报  2 已上报*/
        contract.setId(UUID.randomUUID().toString());
        contract.setState(0);
        contract.setProNum(0);//设置货物的数量
        contract.setExtNum(0);//设置附件的数量
        contract.setTotalAmount(0.0);//设置总金额
        contract.setCreateTime(new Date());//设置创建的时间
        /*保存合同*/
        contractDao.insertSelective(contract);
    }

    @Override
    public void update(Contract contract) {
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void delete(String id) {
        /*查附件数*/
        ExtCproductExample extCproductExample = new ExtCproductExample();
        ExtCproductExample.Criteria criteria = extCproductExample.createCriteria();
        criteria.andContractIdEqualTo(id);
        List<ExtCproduct> extCproducts = extCproductDao.selectByExample(extCproductExample);
        for (ExtCproduct extCproduct : extCproducts) {
            extCproductDao.deleteByPrimaryKey(extCproduct.getId());
        }
        /*查货物数*/
        ContractProductExample contractProductExample=new ContractProductExample();
        ContractProductExample.Criteria criteria1 = contractProductExample.createCriteria();
        criteria1.andContractIdEqualTo(id);
        List<ContractProduct> contractProducts = contractProductDao.selectByExample(contractProductExample);
        for (ContractProduct contractProduct : contractProducts) {
            contractProductDao.deleteByPrimaryKey(contractProduct.getId());
        }
        contractDao.deleteByPrimaryKey(id);
    }

    //1 根据船期获取到excel需要的数据
    @Override
    public List<ContractProductVo> findExcelData(String company_id, String inputDate) {
        List<ContractProductVo> list=    contractDao.findExcelData(company_id,inputDate);
        return list;
    }
}
